﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;

namespace SlovotvorchiKontexty
{
    class Parser
    {
        TextWriter tw_out = null;
        int numSentences = 0;
        public void ProcessFile(FileInfo fi)
        {
            Console.WriteLine("\nProcessing " + fi.Name);
            XmlDocument mydoc = null;
            numSentences = 0;
            try
            {
                mydoc = new XmlDocument();
                mydoc.Load(fi.FullName);

                  tw_out = new StreamWriter("./out/"+fi.Name);



                XmlNodeList nodes = mydoc.SelectNodes("//p");
                
                Console.WriteLine("nodes count = " + nodes.Count);
                for (int num = 0; num < nodes.Count; num++)
                {
                    
                    
                    ProcessBottom(nodes[num].Clone());
                   
                }
                tw_out.WriteLine("Totoal number of sentences with ness + in " + fi.Name + " = " + numSentences.ToString());
                tw_out.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Something went wrong:");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine();

                //Console.WriteLine("file invalid");
            }

        }
        void ProcessBottom(XmlNode source)
        {
            //tw_out.WriteLine("Processinf bottomevel: "+source.InnerText);
            
            XmlNodeList nodes = source.SelectNodes("//w");
            bool isFound = false;
            for (int num = 0; num < nodes.Count; num++)
            {
                string strCurWord = nodes[num].InnerText;
               // tw_out.WriteLine(strCurWord + " ");

                string mask = @"\wness";
                Regex r = new Regex(mask, RegexOptions.Multiline);
                
                if (r.IsMatch(strCurWord))
                {
                    
                    string strCurAttr = nodes[num].Attributes["sem"].Value;
                    XmlNodeList nodesAttr = source.SelectNodes("//w[@sem='" + strCurAttr + "']");
                    if (nodesAttr.Count > 1)
                    {
                        isFound = true;
                        numSentences++;
                        tw_out.WriteLine(strCurWord + " : " + strCurAttr);
                        for (int i = 0; i < nodesAttr.Count; i++)
                        {
                            tw_out.Write(nodesAttr[i].InnerText+" ");
                        }
                        tw_out.WriteLine("\n");
                    }

                }
            }
            if (isFound)
            {
                for (int i = 0; i < nodes.Count; i++)
                {
                    tw_out.Write(nodes[i].InnerText + " ");
                }
                tw_out.WriteLine("\n----------------------------------\n");
            }

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello!");
            DirectoryInfo di = new DirectoryInfo("./in");
            FileInfo[] Files = di.GetFiles();
            try
            {
                if (Directory.Exists("./out")) Directory.Delete("./out", true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("./out :" + ex.Message);
            }
            try { Directory.CreateDirectory("./out"); }
            catch (Exception ex) { Console.WriteLine("./out :" + ex.Message); };

            Parser parser = new Parser();

            foreach (FileInfo fi in Files)
            {
                parser.ProcessFile(fi);
            }

        }
    }
}
